<?php

namespace app\adminapi\controller;

use app\adminapi\logic\media_account\MediaAccountLogic;
use app\common\model\media\Media;
use think\facade\Log;

/**
 * 配置控制器
 * Class ConfigController
 * @package app\adminapi\controller
 */
class OauthController extends BaseAdminController
{
    public array $notNeedLogin = ['callback', 'ksCallback', 'bdCallback', 'vivoCallback'];


    /**
     * 巨量授权回调
     *
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function callback()
    {
        $params   = $this->request->param();
        $authCode = $params['auth_code'] ?? '';
        $authId   = $params['state'] ?? '';

        Log::write('巨量授权回调==>' . json_encode($params), 'info');
        MediaAccountLogic::empower($authCode, (int)$authId, Media::SOURCE_JL);
        header('Location:' . config('media.redirect_url') . '&media=' . Media::SOURCE_JL);
    }


    /**
     * 快手授权回调
     *
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function ksCallback()
    {
        $params   = $this->request->param();
        $authCode = $params['auth_code'] ?? '';
        $authId   = $params['state'] ?? '';

        Log::write('快手授权回调==>' . json_encode($params), 'info');
        MediaAccountLogic::empower($authCode, (int)$authId, Media::SOURCE_KS);
        header('Location:' . config('media.redirect_url') . '&media=' . Media::SOURCE_KS);
    }


    /**
     * 百度授权回调
     *
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function bdCallback()
    {
        $params   = $this->request->param();
        $authCode = $params['authCode'] ?? '';
        $authId   = $params['state'];
        $userId   = $params['userId'] ?? '';

        Log::write('百度授权回调==>' . json_encode($params), 'info');
        MediaAccountLogic::empower($authCode, (int)$authId, Media::SOURCE_BD, $userId);
        header('Location:' . config('media.redirect_url') . '&media=' . Media::SOURCE_BD);
    }


    /**
     * VIVO授权回调
     *
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function vivoCallback()
    {
        $params   = $this->request->param();
        $authCode = $params['code'] ?? '';
        $authId   = $params['state'];
        $clientId = $params['clientId'] ?? '';

        Log::write('VIVO授权回调==>' . json_encode($params), 'info');
        MediaAccountLogic::empower($authCode, (int)$authId, Media::SOURCE_VIVO, $clientId);
        header('Location:' . config('media.redirect_url') . '&media=' . Media::SOURCE_VIVO);
    }
}
